package com.codelinecounter.counter;

import java.util.LinkedList;
import java.util.List;

import com.codelinecounter.rule.CodeLineParser;
import com.codelinecounter.rule.CodeLineParserCreator;
import com.codelinecounter.rule.CodeLineParserCreator.CodeLineParserClass;
import com.codelinecounter.rule.MultipleMiddleCommentCodeLineParser;

public class NoValidCodeLineCounter {
	private List<CodeLineParser> noValidCodeLineParserGroup = new LinkedList<CodeLineParser>();
	private int noValidCodeLineCounter = 0;
	private boolean isNoValidCodeLine = false;

	public NoValidCodeLineCounter() throws Exception {
		CodeLineParser parser = CodeLineParserCreator.createCodeLineParser(CodeLineParserClass.WHITE_CODE_LINE);
		this.noValidCodeLineParserGroup.add(parser);
		parser = CodeLineParserCreator.createCodeLineParser(CodeLineParserClass.SINGLE_COMMENT_CODE_LINE);
		this.noValidCodeLineParserGroup.add(parser);
		CodeLineParser endParser = CodeLineParserCreator.createCodeLineParser(CodeLineParserClass.MULTIPLE_END_COMMENT_CODE_LINE);
		CodeLineParser startParser = CodeLineParserCreator.createCodeLineParser(CodeLineParserClass.MULTIPLE_START_COMMENT_CODE_LINE);
		MultipleMiddleCommentCodeLineParser middleParser = (MultipleMiddleCommentCodeLineParser)CodeLineParserCreator.createCodeLineParser(CodeLineParserClass.MULTIPLE_MIDDLE_COMMENT_CODE_LINE);
		endParser.addStatusWatcher(middleParser);
		startParser.addStatusWatcher(middleParser);
		this.noValidCodeLineParserGroup.add(endParser);
		this.noValidCodeLineParserGroup.add(startParser);
		this.noValidCodeLineParserGroup.add(middleParser);
	}
	
	public void countNoValidCodeLine(String codeLine){
		for (CodeLineParser parser : this.noValidCodeLineParserGroup){
			parser.setCodeLine(codeLine);
			if (parser.parseCodeLine()){
				this.isNoValidCodeLine = true;
				this.noValidCodeLineCounter++;
				break;
			}else{
				this.isNoValidCodeLine = false;
			}
		}
		
	}

	public int getLineCount() {
		return this.noValidCodeLineCounter;
	}

	public boolean isNoValidCodeLine() {
		return this.isNoValidCodeLine;
	}
}
